import { genCmptNo } from '@/utils/helper'
const BLOCKS_ADD_ITEM = 'BLOCKS_ADD_ITEM'
const BLOCKS_DEL_ITEM = 'BLOCKS_DEL_ITEM'
const BLOCKS_RESET = 'BLOCKS_RESET'
const BLOCKS_SET_BLOCKS = 'BLOCKS_SET_BLOCKS'

let I = 0
const state = {
  blocks: []
}

const getters = {
  blocks (state) {
    return state.blocks
  }
}

const actions = {
  setBlocks ({ commit, state, rootState }, blocks) {
    commit(BLOCKS_SET_BLOCKS, blocks)
  },
  addBlockItem ({ commit, state, rootState }, item) {
    commit(BLOCKS_ADD_ITEM, item)
  },
  delBlockItem ({ commit, state, rootState }, idx) {
    commit(BLOCKS_DEL_ITEM, idx)
  },
  resetBlocks ({ commit, state, rootState }) {
    commit(BLOCKS_RESET)
  },
}

const mutations = {
  BLOCKS_RESET (state, item) {
    state.blocks = []
  },
  BLOCKS_ADD_ITEM (state, item) {
    state.blocks.push(item)
  },
  BLOCKS_SET_BLOCKS (state, blocks) {
    state.blocks = blocks
  },
  BLOCKS_DEL_ITEM (state, idx) {
    let t = []
    state.blocks.forEach((n, i) => {
      if (i === idx) return
      t.push(n)
    })
    state.blocks = t
  },
}

export default {
  state,
  getters,
  actions,
  mutations
}
